package com.lianbang.pharmacy.module.medicine.category

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.Menu
import android.view.MenuItem
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import cn.ubest.mvvmcore.base.BaseActivity
import cn.ubest.mvvmcore.utils.ResourcesUtils
import cn.ubest.mvvmcore.utils.ToastUtils
import com.lianbang.pharmacy.R
import com.lianbang.pharmacy.adapter.ProductCategoryListAdapter
import com.lianbang.pharmacy.bean.MedicineCategoryListBean
import com.lianbang.pharmacy.databinding.ActivityProductCategoryBinding
import com.lianbang.pharmacy.vm.MedicineVM

class ProductCategoryActivity : BaseActivity<MedicineVM, ActivityProductCategoryBinding>() {
    private val mAdapter by lazy {
        ProductCategoryListAdapter()
    }
    private val mFragmentList = mutableListOf<Fragment>()

    companion object {
        fun start(context: Context?) {
            context?.also { c ->
                val starter = Intent(c, ProductCategoryActivity::class.java)
                if (c !is Activity) {
                    starter.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                }
                c.startActivity(starter)
            }
        }
    }

    override fun initLayout(): Int {
        return R.layout.activity_product_category
    }

    override fun initData(intent: Intent) {
        mViewModel.mMedicineCategoryLiveData.observe(this, Observer {
            val dataList = mutableListOf<String>()

            for (item in it) {
                dataList.add(item.cate_name)
                mFragmentList.add(ProductCategoryFragment.newInstance(item.child))
            }
            mAdapter.replaceData(dataList)
            selectedFragment(0)
        })
    }

    override fun initView() {
        mBinding.rvList.layoutManager = LinearLayoutManager(this)
        mBinding.rvList.adapter = mAdapter
        mAdapter.setOnItemClickListener { _, _, position ->
            selectedFragment(position)
        }
    }

    private var mLastPosition = -1

    /**
     * 切换tab
     */
    private fun selectedFragment(index: Int) {
        //再次点击不处理
        if (mLastPosition == index || index < 0 || index > mFragmentList.size) {
            return
        }

        val ft: FragmentTransaction = supportFragmentManager.beginTransaction()
        val fragment = mFragmentList[index]
        if (mLastPosition != -1) {
            ft.hide(mFragmentList[mLastPosition])
        }
        if (fragment.isAdded) {
            ft.show(fragment)
        } else {
            ft.add(R.id.fl_container, fragment).show(fragment)
        }
        ft.commitAllowingStateLoss()
        mLastPosition = index
        mAdapter.mCurSelect = index
        mAdapter.notifyDataSetChanged()
    }

    override fun loadData() {
        mViewModel.medicineCategoryList()
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_search, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.btn_search -> ToastUtils.toast("搜索")
        }
        return super.onOptionsItemSelected(item)
    }

    override fun initToolbar() {
        super.initToolbar()
        mTbToolbar?.navigationIcon =
            ResourcesUtils.getResources().getDrawable(R.drawable.icon_toolbar_back)
        mFlToolbar?.setBackgroundColor(ResourcesUtils.getColor(R.color.colorPrimary))
        mTvToolbarTitle?.setTextColor(ResourcesUtils.getColor(R.color.white))
    }

    override fun setToolbarTitle(): String? {
        return "商品分类"
    }
}
